package com.demo.attBcollectionapi;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import com.demo.domain.Name;

public class ComparatorDemo {
	
	public static void main(String[] args) {
		List<Integer> l = Arrays.asList(100, 65, 23, 17, 3, 44);
		//按自然顺序排列
		l.sort(Comparator.naturalOrder());
		System.out.println(l);
		
		//逆序排序
		l.sort(Comparator.reverseOrder());
		System.out.println(l);
		
		List<Integer> l2 = Arrays.asList(100, 65, null, 23, 17, 3, 44);
		//null排最前面
		l2.sort(Comparator.nullsFirst(Comparator.reverseOrder()));
		System.out.println(l2);
		//null排最后面
		l2.sort(Comparator.nullsLast(Comparator.reverseOrder()));
		System.out.println(l2);
		
		List<Name> names = new ArrayList<>();
		names.add(new Name("zhang", "san"));
		names.add(new Name("chen", "li"));
		names.add(new Name("zhao", "wu"));
		names.add(new Name("zhao", "si"));
		names.add(new Name("chen", "nan"));
		names.add(new Name("zhang", "hu"));
		names.add(new Name("li", "na"));
		names.sort(Comparator.comparing(Name::getFirstName).thenComparing(Name::getLastName));
		System.out.println(names);
	}

}
